// pages/flights/flights.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    seatNum:[],
    allPath: [
      "上午7:00 本部--大兴", "中午13:00 本部--大兴",
      "中午11:40 大兴--本部", "下午13:00 大兴--本部"
    ],
    flightList: [
      /**{
        time:"2018-5-22",
        remainingSeats:23,
        path:1,
        seatsNumbers:23,
        id:0,
        shiftId:1,
      }**/
    ],
    
  },
  
  //预约座位
  order:function(event){
    var id = event.detail.target.dataset.cardId-1
    var shiftId = event.detail.target.dataset.shiftId
    var str = "flightList[" + id + "].seatsNumbers"
    var that = this
    let formId =event.detail.formId;
    //console.log(formId);
    wx.request({
      url: 'https://milbtq5q.qcloud.la/reserve/add',
      method:"post",
      data: {
        shiftId,
        formId
      },
      header: { 'content-type': 'application/x-www-form-urlencoded'  },
      success: function (res) {
        //console.log(res.data)
        that.setData({
          [str]: res.data.data.seatNum
        })

      },
      fail: function (res) {
        console.log(res)
      }
    }) 
  },
  //取消预约
  noOrder:function(event) {
    var id = event.currentTarget.dataset.cardId-1
    var shiftId = event.currentTarget.dataset.shiftId
    var str = "flightList[" + id + "].seatsNumbers"
    var that = this
    wx.request({
      url: 'https://milbtq5q.qcloud.la/reserve/cancel',
      method:"post",
      data: {
        shiftId
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
       // console.log(res.data)
        that.setData({
          [str]: res.data.code
        })
      },
      fail: function (res) {
        console.log(res)
      }
    }) 
  },

  formSubmit: function(e) {
    let formId = e.detail.formId;
    console.log(formId);
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
    var that=this
    wx.request({
      url: 'https://milbtq5q.qcloud.la/schedule/lists',
      
      success:function(res){
        console.log(res.data)
        that.setData({
          flightList:res.data.data,
        })
      }
    })
    
    setInterval(function () {
      var k
      var str1 = []
      var ids = []
      var n=that.data.flightList.length
      var urlStr = 'https://milbtq5q.qcloud.la/schedule/getStatus?shiftIds='
      for (k = 0; k <= n-1; k++) {
        urlStr = urlStr + that.data.flightList[k].shiftId
        if (k < n-1) urlStr = urlStr + ','
      }
      //console.log(urlStr)
      wx.request({
        url: urlStr,
        success: function (res) {
          //console.log(res)
          for (k = 0; k <= n-1; k++) {
            str1[k] = "flightList[" + k + "].Departure"
            that.setData({
              [str1[k]]: res.data.data[k].status
            })
          }
        }
      })
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})